<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <cpn @itemclick="cpnclick"></cpn>
    </div>

    <template id="cpn">
        <div>
            <button v-for="item in categories" :key="item.id" @click="btnclick(item)">{{item.name}}</button>
        </div>
    </template>
</body>
<script src='../js/vue.js'></script>
<script>
    // 子组件
    const cpn = {
        template: '#cpn',
        data() {
            return {
                categories: [
                    {id: 'aaa',name: '热门推荐'},
                    {id: 'bbb',name: '数码设备'},
                    {id: 'ccc',name: '家用电器'},
                    {id: 'ddd',name: '美妆美肤'}
                ]
            }
        },
        // 子传父用$emit
        methods: {
            btnclick(item){
                // console.log(item);
                this.$emit('itemclick',item)
            }
        },
    }
    // 父组件
    let app = new Vue({
        el: '#app',
        data: {
             msg:'hello vue.js'
        },
        methods: {
            cpnclick(item){
                console.log('cpnclick',item);
            }
        },
        components: {
            cpn
        }
    })
</script>
</html>